package test.hxy.mongoSchoolSimple.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import test.hxy.mongoSchoolSimple.dao.DepartmentOneDao;
import test.hxy.mongoSchoolSimple.dao.DepartmentTwoDao;
import test.hxy.mongoSchoolSimple.model.Department;
import test.hxy.mongoSchoolSimple.model.Leader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentService {
    @Autowired
    DepartmentOneDao departmentDao;
    @Autowired
    DepartmentTwoDao departmentTwoDao;
    public List<Department> getDepartmentByVacanciesBetween(Integer num1, Integer num2) {
        return departmentDao.findByVacanciesBetween(num1, num2);
    }
    public List<Department> getDepartmentByHeadcountLessThanOrHeadcountGreaterThan(Integer num1, Integer num2) {
        return departmentDao.findByHeadcountLessThanOrHeadcountGreaterThan(num1, num2);
    }
    public List<Department> getDepartmentByNameIn(List<String> names) {
        return departmentDao.findByNameIn(names);
    }
    public List<Department> getDepartmentByTypeIn(Integer type, int page, int size) {
        if (page < 1) {
            page = 1;
        }
        Pageable pageable = PageRequest.of(page - 1, size);
        return departmentDao.findByTypeIn(type, pageable);
    }
//    public List<Department> getDepartmentByLeaderName(String name) {
//        return departmentDao.findByLeaderName(name);
//    }
    public List<Department> getDepartmentBysub(String[] names) {
        return departmentTwoDao.findallnames(names);
    }
    public List<Department> getDepartmentByleaders(String position, String title) {
        return departmentTwoDao.findleaders(position, title);
    }
    public Map<String, String> insertDepartment(Department department){
        Map<String, String> map = new HashMap<String, String>();
        map.put("mode","insert one department ");
        try{
            departmentTwoDao.insertDepartment(department);
            map.put("status","success");
        }   catch (Exception e) {
            map.put("status",e.getMessage());
        }
        return map;
    }
    public Map<String, String> upsertLeader(Leader leader,String id){
        Map<String, String> map = new HashMap<String, String>();
        map.put("mode","insert one Leader ");
        try{
            departmentTwoDao.upsertLeader(leader,id);
            map.put("status","success");
        }   catch (Exception e) {
            map.put("status",e.getMessage());
        }
        return map;
    }
    public Map<String, String> deleteDepartment(String name){
        Map<String, String> map = new HashMap<String, String>();
        map.put("mode","delete one department ");
        try{
            departmentTwoDao.deleteDepartment(name);
            map.put("status","success");
        }   catch (Exception e) {
            map.put("status",e.getMessage());
        }
        return map;
    }

    public List<Department> getAll(){
        return departmentDao.findAll();
    }
}